package com.example.newbilityblog.blog.service;

import com.example.newbilityblog.blog.api.response.BlogResponse;
import com.example.newbilityblog.blog.infrastructure.repository.BlogJpaRepository;
import com.example.newbilityblog.blog.service.mapper.BlogMapper;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

@Service
public class BlogRepresentationService {

    private final BlogJpaRepository blogJpaRepository;
    private final BlogMapper mapper;

    public BlogRepresentationService(BlogJpaRepository blogJpaRepository, BlogMapper mapper) {
        this.blogJpaRepository = blogJpaRepository;
        this.mapper = mapper;
    }

    public Page<BlogResponse> getBlogs(Pageable pageable) {
        return blogJpaRepository.findAll(pageable)
                .map(mapper::toBlogResponse);
    }

    public BlogResponse getBlog(Long blogId) {
        return mapper.toBlogResponse(
                blogJpaRepository.findById(blogId)
                        .orElseThrow(RuntimeException::new));
    }

    public long count() {
        return blogJpaRepository.count();
    }
}
